M <- matrix(1:20, ncol = 5, nrow = 4, byrow = TRUE)
print(apply(M, MARGIN = 2, FUN = mean)) ## Compute the mean of each column[1] 8.5 9.5 10.5 11.5 12.5
[1] 3 8 13 18
Lecture 4
1 2 3
0.1109914 0.5669564 1.0563289
NULL will be used;x. By default, compute the Euclidean norm (p = 2).Best practice in programming with R would be to dedicate a script (within each project) to all written functions and call it with source(). This way, functions can be easily reused in multiple scripts.
Even better, even if it requires a little bit more work at the beginning, is to set up an R package. A very good reference on how to do it is here
\[ \hat{\beta} = (X^T X)^{-1}X^T y \]
(Refer to regression courses for this).
Then, you can save the code for getting this in a function stored in a file called my_regr_coeff.R and use source("my_regr_coeff.R") to load it in the environment
The function might be the following
my_rc <- function(X, Y, add_intercept = TRUE) {
n <- length(X)
if (add_intercept) {
X2 <- cbind(rep(1, n), X)
## t(X) returns the transpose of X
## solve(X) returns the inverse of X
b <- solve(t(X2) %*% X2) %*% t(X2) %*% Y
}
if (!add_intercept) {
b <- solve(t(X) %*% X) %*% t(X) %*% Y
names(b) <- "beta_1"
}
if (length(b) > 1)
return(list(beta_0 = b[1], beta_1 = b[2]))
if (length(b) == 1)
return(list(beta_1 = b[1]))
}Suppose you want to get the coefficients of \(\hat{\beta}\) with X = height and Y = weights of some people.
We use this code:
X mpg cyl disp hp drat wt qsec vs am gear carb
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
covid_daily_report = read.csv(file = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/jhu/full_data.csv")
head(covid_daily_report) date location new_cases new_deaths total_cases total_deaths
1 2020-02-24 Afghanistan 5 NA 5 NA
2 2020-02-25 Afghanistan 0 NA 5 NA
3 2020-02-26 Afghanistan 0 NA 5 NA
4 2020-02-27 Afghanistan 0 NA 5 NA
5 2020-02-28 Afghanistan 0 NA 5 NA
6 2020-02-29 Afghanistan 0 NA 5 NA
weekly_cases weekly_deaths biweekly_cases biweekly_deaths
1 NA NA NA NA
2 NA NA NA NA
3 NA NA NA NA
4 NA NA NA NA
5 NA NA NA NA
6 5 NA NA NA
## retrieve the historical Gross Domestic Product for Japan
x <- getSymbols(Symbols = 'JPNNGDP', src = 'FRED', auto.assign = FALSE)
tail(x) JPNNGDP
2023-01-01 583288.0
2023-04-01 594937.2
2023-07-01 594562.4
2023-10-01 598615.8
2024-01-01 597132.0
2024-04-01 607903.7